package com.xiaomi.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.dao.GoodsDao;
import com.xiaomi.dao.GoodsTypeDao;
import com.xiaomi.pojo.Goods;
import com.xiaomi.pojo.GoodsType;
import com.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Author xiao宋
 * @Date 15:57  2022/9/14
 * @Version 1.0
 * @Description TODO
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    GoodsTypeDao goodsTypeDao;
    @Override
    public Goods findById(int goodsId) {
        Goods goods = goodsDao.findById(goodsId);

        int typeid = goods.getTypeid();

        GoodsType goodsType = goodsTypeDao.findById(typeid);
        goods.setGoodsType(goodsType);
        return goods;
    }

    @Override
    public PageInfo<Goods> findGoodsListByTypeIdPage(int goodsTypeId,Integer pageNum, Integer pageSize) {
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
//        List<Goods> list=goodsDao.findAll();

        List<Goods> list = goodsDao.findGoodsByTypeId(goodsTypeId);
//        System.out.println(list);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public List<Goods> getGoodsList() {
        // 查到商品信息。
        List<Goods>list= goodsDao.findAll();
        //通过typeid 查到typename
        for (Goods goods : list) {
            String typeName= goodsDao.findById(goods.getTypeid()).getName();
            goods.setTypeName(typeName);
        }
        return list;
    }

    @Override
    public void addGoods(String name, String picture, String intro, int price, int star, String pubdate, int typeid) {
        goodsDao.addGoods(name,picture,intro,price,star,pubdate,typeid);
    }


}
